<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>代理管理</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
</head>
<body>
<div id="main">
    <div id="toolbar">
        <shiro:hasPermission name="upms:agent:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createAction()"><i class="zmdi zmdi-plus"></i> 新增代理</a></shiro:hasPermission>
        <shiro:hasPermission name="upms:agent:update"><a class="waves-effect waves-button" href="javascript:;" onclick="updateAction()"><i class="zmdi zmdi-edit"></i> 编辑代理</a></shiro:hasPermission>
        <shiro:hasPermission name="upms:agent:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-close"></i> 删除代理</a></shiro:hasPermission>
    </div>
    <table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<script>
    var $table = $('#table');
    $(function() {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/agent/list',
            height: getHeight(),
            dataField:"rows",
            search: true,
            showRefresh: true,
            showColumns: true,
            clickToSelect: true,
            pagination: false,
            treeView: true,
            treeId: "id",
            treeField: "name",
            treeCollapseAll:"false",
            pagination: false,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'id',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'name', title: '名称'},
                {field: 'agenttype', title: '代理类型',formatter: function (value, row, index) {
                        return getDictText("UPMS_AGENT_TYPE",value);
                    }},
                {field: 'usethirdapp', title: '是否使用独立小程序',formatter: function (value, row, index) {
                        return getDictText("UPMS_YES_NO",value);
                    }},
                {field: 'rate', title: '营业额扣除百分比(%)'},
                {field: 'description', title: '描述'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: true}
            ]
        });
    });
    window.actionEvents = {
        <shiro:hasPermission name="upms:agent:create">
        'click .create': function(e, value, row, index) {
            createAction([row]);
        },
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:agent:update">
        'click .update': function(e, value, row, index) {
            updateAction([row]);
        },
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:agent:delete">
        'click .delete': function(e, value, row, index) {
            deleteAction([row]);
        }
        </shiro:hasPermission>
    };

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        if(!row.pid)
            return [
                <shiro:hasPermission name="upms:agent:create">
                '<a class="create" href="javascript:;" data-toggle="tooltip" title="Add"><i class="glyphicon glyphicon-plus"></i></a>　'
                </shiro:hasPermission>
            ].join('');
        return [
            <shiro:hasPermission name="upms:agent:create">
            '<a class="create" href="javascript:;" data-toggle="tooltip" title="Add"><i class="glyphicon glyphicon-plus"></i></a>　',
            </shiro:hasPermission>
            <shiro:hasPermission name="upms:agent:update">
            '<a class="update" href="javascript:;" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>　',
            </shiro:hasPermission>
            <shiro:hasPermission name="upms:agent:delete">
            '<a class="delete" href="javascript:;" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
            </shiro:hasPermission>
        ].join('');
    }
    // 新增
    var createDialog;
    function createAction(rows) {
        var url = 'url:${basePath}/manage/agent/create/-1';
        if(rows){
            url = 'url:${basePath}/manage/agent/create/'+rows[0].id;
        }
        createDialog = openDialog(url,'新增代理')
    }
    // 编辑
    var updateDialog;
    function updateAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            infoDialog('请选择一条记录！');
        } else {
            if(!rows[0].pid){
                infoDialog( '总代理不可编辑！');
                return;
            }
            updateDialog = openDialog('url:${basePath}/manage/agent/update/'+rows[0].id,'编辑代理');
        }
    }
    // 删除
    var deleteDialog;
    function deleteAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            infoDialog( '请至少选择一条记录！');
        } else {
            deleteDialog = confirmDialog('确认删除该代理吗？',function(){
                var ids = new Array();
                for (var i in rows) {
                    if(!rows[i].pid){
                        infoDialog( '总代理不可删除！');
                        return;
                    }
                    ids.push(rows[i].id);
                }
                $.ajax({dataType: 'json',
                    type: 'get',
                    url: '${basePath}/manage/agent/delete/' + ids.join("-"),
                    success: function(result) {
                        if (result.code != 1) {
                            if (result.data instanceof Array) {
                                $.each(result.data, function(index, value) {
                                    infoDialog(value.errorMsg);
                                });
                            } else {
                                infoDialog(result.data);
                            }
                        } else {
                            deleteDialog.close();
                            $table.bootstrapTable('refresh');
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        infoDialog(textStatus);
                    }
                });
            });
        }
    }
</script>
</body>
</html>